<?php
header('Content-Type: application/json');
	require_once("util.php");
	require_once("sql.php");
	require_once("document.php");
	require_once("managestatutorydocumentsmapping.php");
?>

<?php

$statutorydocumentmappingmanager = new ManageStatutoryDocumentsMapping();

if($_SERVER['REQUEST_METHOD'] == "GET"){

	$responseJson="";
	
	//Get the fulldetils of roles(for display in table)
	if (array_key_exists("fulldetails",$_REQUEST)){
	
	}
	else {//for update & delete operation, get the information about the selected role
		if (array_key_exists("key",$_REQUEST)){
		
		$documentPK= $_REQUEST["key"];		
			
			//GET ALL MAPPED STATUTORIES
			$mappings = $statutorydocumentmappingmanager->getStatutoryMappedToDocument($documentPK);
			$statutoryJsonList=array();
			foreach($mappings as $mapping){
				$statutoryJsonString ='{';
				$statutoryJsonString .= '"value":"' .$mapping->getId() .'",';
				$statutoryJsonString .= '"checked":"true",';
				$statutoryJsonString .= '"text":"' .$mapping->getName() .'"';
				$statutoryJsonString .='}';
				array_push($statutoryJsonList,$statutoryJsonString);
			}
			
			//GET ALL UNMAPPED STATUTORIES
			$unmappedstatutories = $statutorydocumentmappingmanager->getStatutoryNotMappedToDocument($documentPK);
			$unmappedstatutoryJsonList=array();
			foreach($unmappedstatutories as $unmappedmapping){
				$statutoryJsonString ='{';
				$statutoryJsonString .= '"value":"' .$unmappedmapping->getId() .'",';
				$statutoryJsonString .= '"checked":"false",';
				$statutoryJsonString .= '"text":"' .$unmappedmapping->getName() .'"';
				$statutoryJsonString .='}';
				array_push($statutoryJsonList,$statutoryJsonString);
			}
			
			$statjsonStr=implode(",",$statutoryJsonList);
			
			$responseJson='[{"id":"document_id","label":"Document","display":"true","value":"' .$documentPK .'","type":"input_text"} ,{"id":"document_name","label":"Name","display":"true","value":"' .$documentPK .'","type":"input_text"},{"id":"statutory_list","label":"Statutories","display":"true","type":"checkbox_group","value":[' .$statjsonStr .']}]';
		
		}
		else {//For create operation
		
		}

}
	echo $responseJson;
}
elseif ($_SERVER['REQUEST_METHOD'] == "POST"){

	if (array_key_exists("action",$_REQUEST)){
			$action= $_REQUEST["action"];
			
			$success ='{"status" :"success"}';
			$failure ='{"status" :"faiure"}';
			$status=true;
			if($action=="create"){
				
			}elseif($action=="update"){
				$status=$statutorydocumentmappingmanager->updateStatutoryDocumentMapping($_REQUEST["document_id"],isset($_REQUEST["statutory_list"])?$_REQUEST["statutory_list"]:null);
				if($status){echo $success;}
				else {echo $failure;}
			}else if($action=="delete"){
				
			}else{
				echo $failure;
			}
			
		}else{
			echo $failure;
		}
}

?>